<?php

class UsersTemplate{
	public static function getForm($user){
		?>
		<h3>Campos Adicionales</h3>

        <table class="form-table">
            <tr>
                <th><label for="name_profile">Nombre de la Empresa</label></th>
                <td><input type="text" name="name_profile" value="<?php echo esc_attr(get_the_author_meta( 'name_profile', $user->ID )); ?>" class="regular-text" /></td>
            </tr>           
            <tr>
                <th><label for="ruc_profile">RUC</label></th>
                <td><input type="text" name="ruc_profile" maxlength="12" value="<?php echo esc_attr(get_the_author_meta( 'ruc_profile', $user->ID )); ?>" class="regular-text" /></td>
            </tr>
            <tr>
                <th><label for="rubro_profile">Rubro</label></th>
                <td><?php 
                wp_dropdown_categories(
                	array(
                		'taxonomy'	 => 'listing',
                		'selected'	 => get_the_author_meta( 'rubro_profile', $user->ID ),
                		'hide_empty' => 0,
                		'depth'	     => 1,
                		'name'		 => 'rubro_profile',
                		)
                	);?></td>
            </tr>
            <tr>
                <th><label for="subrubro_profile">Sub Rubro</label></th>
                <td><?php 
                wp_dropdown_categories(
                	array(
                		'taxonomy'	 => 'listing',
                		'selected'	 => get_the_author_meta( 'subrubro_profile', $user->ID ),
                		'hide_empty' => 0,
                		'depth'	     => 0,
                		'name'		 => 'subrubro_profile',
                		)
                	);?></td>
            </tr>
            <tr>
                <th><label for="number_profile">Nro. Colaboradores</label></th>
                <td><input type="text" name="number_profile" value="<?php echo esc_attr(get_the_author_meta( 'number_profile', $user->ID )); ?>" class="regular-text" /></td>
            </tr>
        </table>
    <?php
	}

	public static function saveForm($user_id){
		 update_user_meta( $user_id,'name_profile', sanitize_text_field( $_POST['name_profile'] ) );
		 update_user_meta( $user_id,'ruc_profile', sanitize_text_field( $_POST['ruc_profile'] ) );
		 update_user_meta( $user_id,'rubro_profile', sanitize_text_field( $_POST['rubro_profile'] ) );
		 update_user_meta( $user_id,'subrubro_profile', sanitize_text_field( $_POST['subrubro_profile'] ) );
		 update_user_meta( $user_id,'number_profile', sanitize_text_field( $_POST['number_profile'] ) );	
	}
    public static function showExportsUserForm(){
        ?>
         <h2>Exportar data de usuarios</h2>

    

            <form enctype="multipart/form-data" action="" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
            <br/>

            <p>Escoja el a&ntilde;o a exportar</p>
            <select name="year">
                <option value="0">Todos</option>
                <?php
                $years = range(2000,date('Y') );;
                foreach($years as $year){
                    echo '<option  name="year" value="'.$year.'">'.$year.'</option>';
                }
                ?>

            </select>
            
            </p><hr />
            <p>Escoja el rubro a exportar</p>
             <select name="rubro">
            <option value="0">Todos</option>
                <?php
                $terms = get_terms('listing',array('parent'=>0,'hide_empty'=>0));
                    for($i=0;$i<count($terms);$i++){
                        echo '<option name="rubro" value="' .$terms[$i]->term_id . '">'.$terms[$i]->name.'</option>';
                    }
                
                ?>
              </select>
            <p class="submit">
            <input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
            </p>

            </form>
            </div>
<?php
    }
    public static function showImportUsersForm(){
        $data_field_name = "filename";
        $msg = "";
        $base_msg= " %d Usuarios importados correctamente";

        //Validate the file uploaded 
        if(isset($_FILES) and isset($_FILES[$data_field_name])){
            $counter = 0;
            if($_FILES[$data_field_name]['error'] == UPLOAD_ERR_OK){
                $file_handle = fopen($_FILES[$data_field_name][tmp_name], "r");
                while (!feof($file_handle) ) {
                    $column = fgetcsv($file_handle, 1024, ";");
                    if($column[0] == "Razon social" or $column[1] == "Nro. Documento"){}else
                    {
                        
                        $email =  $column[3];
                        $name  =  $column[0];
                        $rubro =  $column[2];
                        $ruc   =  $column[1];
                        $role  =  "Empresa";
                        $term = get_term_by('name', $rubro, 'listing');
                        $rubro_id = $term->term_taxonomy_id;
                      
                        if(isset($column[4])){
                            $number = $column[4];
                        }else{
                            $number = 0;
                        }
                        $user_data =  array(
                            'user_login'  =>  $ruc,
                            'user_name'   =>  $name,
                            'user_email'  =>  $email,
                            'user_pass'   =>  wp_generate_password()  // When creating an user, `user_pass` is expected.
                        );

                        $user_id = wp_insert_user( $user_data );
                        if( !is_wp_error($user_id) ) {
                             wp_update_user( array ('ID' => $user_id, 'role' => 'company' ) ) ;
                             
                             wp_new_user_notification( $user_id, $user_data["user_pass"] );
                             
                             update_user_meta( $user_id,'name_profile', sanitize_text_field( $name ) );
                             update_user_meta( $user_id,'ruc_profile', sanitize_text_field( $ruc ) );
                             update_user_meta( $user_id,'rubro_profile', sanitize_text_field( $rubro_id ) );
                             update_user_meta( $user_id,'number_profile', sanitize_text_field( $number ) );
                            $counter++;
                        }
                    }
                    
                }
                fclose($file_handle);
                $msg = sprintf($base_msg, $counter);
            }else{
                $msg = "Tuvimos un problema procesando el csv";
            }
        }
        ?>
    <div class="wrap">
        <?php
        if($msg != ""){
            ?>
<div id='notice' class='updated fade'><p><?php echo $msg;?>.</p></div>

<?php } ?>
    <h2>Importar usuarios</h2>

    

<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<br/>

<p>Seleccione un archivo csv con el formato para importar la data</p>
<input type="file" name="<?php echo $data_field_name; ?>" value="Subir archivos" size="20">
</p><hr />

<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>

</form>
</div>

        <?php
    }
}